﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Linq.Expressions;
using Newtonsoft.Json;
using OA.Core;
using OA.Model;
using OA.BLL;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.Web.Code;
using Microsoft.AspNetCore.Http;
using OA.Core.Helper;
using OA.Core.Oss;

namespace OA.Web.Areas.Admin.Controllers
{
    public class SysConfigController : BaseAdminController
    {

        private IMemoryCache _cache;
        public SysConfigController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        public IActionResult Index()
        {            
            return View();
        }

        public IActionResult GetData(string keyword, int page = 1, int limit = 20)
        {
            Expression<Func<SysConfig, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = m => (m.Key.Contains(keyword) || m.Name.Contains(keyword));
            }
            PageList<SimpleSysConfig> list = this.GetPageList<SysConfig, SimpleSysConfig>(where, page, limit, SimpleSysConfig.Map);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.RowCount,
                data = list.List
            };
            return Json(result);
        }

        /// <summary>
        /// 网站基本配置
        /// </summary>
        /// <returns></returns>
        public IActionResult SetBaseConfig()
        {
            Expression<Func<SysConfig, bool>> where = m => m.Key.ToLower() == "BaseConfig".ToLower();
            SysConfig info = this.GetInfo<SysConfig>(where);
            BaseConfig config = new BaseConfig();
            if (info != null && !string.IsNullOrWhiteSpace(info.Content))
            {
                config = JsonHelper.DeserializeObj<BaseConfig>(info.Content);
                if (config == null) { config = new BaseConfig(); }
            }
            return View(config);
        }

        /// <summary>
        /// 网站基本配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SetBaseConfig(BaseConfig config)
        {
           
            Expression<Func<SysConfig, bool>> where = m => m.Key.ToLower() == "BaseConfig".ToLower();
            SysConfig info = this.GetInfo<SysConfig>(where);
            if (info != null)
            {
                SysConfig updateInfo = new SysConfig();
                updateInfo.Content = JsonConvert.SerializeObject(config);
                this.Update<SysConfig>(info, updateInfo, new string[] { "Content" });
            }
            else
            {
                info = new SysConfig();
                info.Key = "BaseInfoConfig";
                info.Name = "基础配置";
                info.Content = JsonConvert.SerializeObject(config);
                this.AddInfo<SysConfig>(info);
            }
            _cache.Set("Config.BaseInfoConfig".ToLower(), config);

            BaseResult baseResult = new BaseResult();
            baseResult.Msg = "基础配置修改成功";
            return Json(baseResult);
        }


        /// <summary>
        /// 网站基本配置
        /// </summary>
        /// <returns></returns>
        public IActionResult SetIcoConfig()
        {
            Expression<Func<SysConfig, bool>> where = m => m.Key.ToLower() == "IcoConfig".ToLower();
            SysConfig info = this.GetInfo<SysConfig>(where);
            OA.Model.Config.IcoConfig config = new OA.Model.Config.IcoConfig();
            if (info != null && !string.IsNullOrWhiteSpace(info.Content))
            {
                config = JsonHelper.DeserializeObj<OA.Model.Config.IcoConfig>(info.Content);
                if (config == null) { config = new OA.Model.Config.IcoConfig(); }
            }
            return View(config);
        }

        /// <summary>
        /// 网站基本配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SetIcoConfig(OA.Model.Config.IcoConfig config)
        {
            Expression<Func<SysConfig, bool>> where = m => m.Key.ToLower() == "IcoConfig".ToLower();
            SysConfig info = this.GetInfo<SysConfig>(where);
            if (info != null)
            {
                SysConfig updateInfo = new SysConfig();
                updateInfo.Content = JsonConvert.SerializeObject(config);
                this.Update<SysConfig>(info, updateInfo, new string[] { "Content" });
            }
            else
            {
                info = new SysConfig();
                info.Key = "IcoInfoConfig";
                info.Name = "图标配置";
                info.Content = JsonConvert.SerializeObject(config);
                this.AddInfo<SysConfig>(info);
            }
            _cache.Set("Config.IcoInfoConfig".ToLower(), config);
            BaseResult baseResult = new BaseResult();
            baseResult.Msg = "图标配置修改成功";
            return Json(baseResult);
        }

        /// <summary>
        /// 阿里云OSS配置
        /// </summary>
        /// <returns></returns>
        public IActionResult SetOssConfig()
        {
            Expression<Func<SysConfig, bool>> where = m => m.Key.ToLower() == "OSSConfig".ToLower();
            SysConfig info = this.GetInfo<SysConfig>(where);
            OssConfig config = new OssConfig();
            if (info != null && !string.IsNullOrWhiteSpace(info.Content))
            {
                config = JsonHelper.DeserializeObj<OssConfig>(info.Content);
                if (config == null) { config = new OssConfig(); }
            }
            return View(config);
        }

        /// <summary>
        /// 阿里云OSS配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SetOssConfig(OssConfig config)
        {
            Expression<Func<SysConfig, bool>> where = m => m.Key.ToLower() == "OSSConfig".ToLower();
            SysConfig info = this.GetInfo<SysConfig>(where);
            if (info != null)
            {
                SysConfig updateInfo = new SysConfig();
                updateInfo.Content = JsonConvert.SerializeObject(config);
                this.Update<SysConfig>(info, updateInfo, new string[] { "Content" });
            }
            else
            {
                info = new SysConfig();
                info.Key = "OssInfoConfig";
                info.Name = "阿里云OSS";
                info.Content = JsonConvert.SerializeObject(config);
                this.AddInfo<SysConfig>(info);
            }
            _cache.Set("Config.BaseInfoConfig".ToLower(), config);
            BaseResult baseResult = new BaseResult();
            baseResult.Msg = "阿里云OSS配置修改成功";
            return Json(baseResult);
        }



        /// <summary>
        /// 微信小程序配置
        /// </summary>
        /// <returns></returns>
        public IActionResult SetWeiXinConfig()
        {
            Expression<Func<SysConfig, bool>> where = m => m.Key.ToLower() == "WeiXinConfig".ToLower();
            SysConfig info = this.GetInfo<SysConfig>(where);
            WeiXinConfig config = new WeiXinConfig();
            if (info != null && !string.IsNullOrWhiteSpace(info.Content))
            {
                config = JsonHelper.DeserializeObj<WeiXinConfig>(info.Content);
                if (config == null) { config = new WeiXinConfig(); }
            }
            return View(config);
        }

        /// <summary>
        /// 微信小程序配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SetWeiXinConfig(WeiXinConfig config)
        {
            Expression<Func<SysConfig, bool>> where = m => m.Key.ToLower() == "WeiXinConfig".ToLower();
            SysConfig info = this.GetInfo<SysConfig>(where);
            if (info != null)
            {
                SysConfig updateInfo = new SysConfig();
                updateInfo.Content = JsonConvert.SerializeObject(config);
                this.Update<SysConfig>(info, updateInfo, new string[] { "Content" });
            }
            else
            {
                info = new SysConfig();
                info.Key = "WeiXinConfig";
                info.Name = "微信小程序配置";
                info.Content = JsonConvert.SerializeObject(config);
                this.AddInfo<SysConfig>(info);
            }
            _cache.Set("Config.WeiXinConfig".ToLower(), config);
            BaseResult baseResult = new BaseResult();
            baseResult.Msg = "微信小程序配置修改成功";
            return Json(baseResult);
        }


    }
}